<template>
  <el-form ref="deptForm" label-width="120px">
    <el-form-item label="部门名称">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeesPeople, addDepartments } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 表单需要的数据
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 保存部门负责人
      list: []
    }
  },
  // 钩子
  created() {
    this.loadgetEmployeesPeople()
  },
  // 方法：
  methods: {
    // 新增子部门
    async doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        console.log(res, 'addDepartments')
        // 子传父----> 关闭弹层，刷新列表
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取部门负责人信息
    async loadgetEmployeesPeople() {
      try {
        const res = await getEmployeesPeople()
        console.log(res, 'getEmployeesPeople')
        // 数据绑定
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.doAdd()
    },
    hCancel() {}
  }
}
</script>

<style>
</style>
